/*----------------------------------------------------------------------------
 * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#include "stdlib.h"
#include "los_mux.h"
#include "los_mux_debug_pri.h"
#include "los_task_pri.h"
#include "pthread.h"
#include "errno.h"
#include "string.h"
#include "los_priqueue_pri.h"
#include "map_error.h"
#include "los_bitmap.h"

#if (LOSCFG_BASE_CORE_SWTMR == YES)
#include "los_exc.h"
#include "los_swtmr_pri.h"
#endif
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

#define PTHREADS_MUTEX_CHECK(ptr)   if ((ptr) == NULL) return EINVAL
#define MUTEXATTR_TYPE_MASK 0x0f

static UINT32 OsMuxPendForPosix(pthread_mutex_t *mutex, UINT32 timeout);
static UINT32 osMuxPostForPosix(pthread_mutex_t *mutex);

int pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
    if (attr == NULL) {
        return EINVAL;
    }

#if defined POSIX_MUTEX_DEFAULT_INHERIT
    attr->protocol    = PTHREAD_PRIO_INHERIT;
#elif defined POSIX_MUTEX_DEFAULT_PROTECT
    attr->protocol    = PTHREAD_PRIO_PROTECT;
#else
    attr->protocol    = PTHREAD_PRIO_NONE;
#endif
    attr->prioceiling = OS_TASK_PRIORITY_LOWEST;
    attr->type        = PTHREAD_MUTEX_DEFAULT;
    return ENOERR;
}

//-----------------------------------------------------------------------------
// Destroy attribute object
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
{
    if (attr == NULL) {
        return EINVAL;
    }

    return ENOERR;
}

//-----------------------------------------------------------------------------
// Optional functions depending on priority inversion protection options.

// Set priority inversion protection protocol
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)
{
    if (attr == NULL) {
        return EINVAL;
    }

    switch (protocol) {
    case PTHREAD_PRIO_NONE:
    case PTHREAD_PRIO_INHERIT:
    case PTHREAD_PRIO_PROTECT:
        attr->protocol = (unsigned char)protocol;
        return ENOERR;

    default:
        return EINVAL;
    }
}

// Get priority inversion protection protocol
int pthread_mutexattr_getprotocol(pthread_mutexattr_t *attr, int *protocol)
{
    if (attr != NULL && protocol != NULL) {
        *protocol = attr->protocol;
    } else {
        return EINVAL;
    }

    return ENOERR;
}

// Set priority for priority ceiling protocol
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)
{
    if (attr == NULL ||
        prioceiling < OS_TASK_PRIORITY_HIGHEST ||
        prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }

    attr->prioceiling = (unsigned char)prioceiling;

    return ENOERR;
}

// Get priority for priority ceiling protocol
int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *attr, int *prioceiling)
{
    if (attr == NULL) {
        return EINVAL;
    }

    if (prioceiling != NULL) {
        *prioceiling = attr->prioceiling;
    }

    return ENOERR;
}

// Set priority ceiling of given mutex, returning old ceiling.
int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling)
{
    int ret = ENOERR, retLock = ENOERR;
    if (mutex == NULL ||
        prioceiling < OS_TASK_PRIORITY_HIGHEST ||
        prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }

    retLock = pthread_mutex_lock(mutex);
    if (retLock != ENOERR) {
        return retLock; /*lint !e454*/
    }

    if (old_ceiling != NULL) {
        *old_ceiling = mutex->stAttr.prioceiling;
    }

    ret = pthread_mutexattr_setprioceiling(&mutex->stAttr, prioceiling);

    retLock = pthread_mutex_unlock(mutex);
    if (ret == ENOERR && retLock != ENOERR) {
        return retLock;
    }

    return ret;
}

// Get priority ceiling of given mutex
int pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *prioceiling)
{
    if (mutex != NULL && prioceiling != NULL) {
        *prioceiling = mutex->stAttr.prioceiling;
    } else {
       return EINVAL;
    }

    return ENOERR;
}

int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p)
{
    int type;

    if (attr == NULL || type_p == NULL) {
        return EINVAL;
    }

    type = (attr->type& MUTEXATTR_TYPE_MASK);

    if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }

    *type_p = type;

    return ENOERR;
}

int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
{
    if (attr == NULL ||
        type < PTHREAD_MUTEX_NORMAL ||
        type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }

    attr->type = (attr->type & ~MUTEXATTR_TYPE_MASK) | (unsigned)type;
    return ENOERR;
}


//-----------------------------------------------------------------------------
// Mutex functions

//-----------------------------------------------------------------------------
// Initialize mutex. If mutex_attr is NULL, use default attributes.
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr)
{
    pthread_mutexattr_t use_attr;
    MUX_CB_P *pstMuxCreated = (MUX_CB_P *)NULL;

    if (mutex == NULL) {
        return EINVAL;
    }

    /* Set up the attributes we are going to use */
    if (mutex_attr == NULL) {
        if (pthread_mutexattr_init( &use_attr ) != ENOERR) {
            return EINVAL;
        }
    } else {
        use_attr = *mutex_attr;
    }

    if ((INT8)(use_attr.type) < PTHREAD_MUTEX_NORMAL ||
        use_attr.type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }
    if ((INT8)(use_attr.prioceiling) < OS_TASK_PRIORITY_HIGHEST ||
        use_attr.prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }
    if ((INT8)(use_attr.protocol) < PTHREAD_PRIO_NONE ||
        use_attr.protocol > PTHREAD_PRIO_PROTECT) {
        return EINVAL;
    }

    mutex->stAttr = use_attr;
    pstMuxCreated = &mutex->stLock;

    pstMuxCreated->usMuxCount  = 0;
    pstMuxCreated->pstOwner    = (LosTaskCB *)NULL;
    LOS_ListInit(&pstMuxCreated->stMuxList);

    return ENOERR;
}

//-----------------------------------------------------------------------------
// Destroy mutex.
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
    MUX_CB_P *pstMuxDeleted = (MUX_CB_P *)NULL;

    if (mutex == NULL) {
    	return EINVAL;
    }

    pstMuxDeleted = &mutex->stLock;

    if (pstMuxDeleted->usMuxCount != 0) {
        return EBUSY;
    }
    (void)memset_s(mutex, sizeof(pthread_mutex_t), 0, sizeof(pthread_mutex_t));

    return ENOERR;
}

//-----------------------------------------------------------------------------
// Lock mutex, waiting for it if necessary.
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
    MUX_CB_P *muxPended = (MUX_CB_P *)NULL;
    LosTaskCB *runTsk = (LosTaskCB *)NULL;
    UINT32 ret;

    if (mutex == NULL) {
        return EINVAL;
    }

    if (OS_INT_ACTIVE) {
        return EPERM;
    }

#if (LOSCFG_BASE_CORE_SWTMR == YES)
     /* DO NOT Call blocking API in software timer task */
    if (g_losTask.runTask->taskEntry == (TSK_ENTRY_FUNC)OsSwtmrTask)
    {
        PRINTK("Warning: DO NOT call %s in software timer callback.\n", __FUNCTION__);
        osBackTrace();
    }
#endif

    muxPended = &mutex->stLock;
    runTsk = g_losTask.runTask;

    if (mutex->stAttr.type == PTHREAD_MUTEX_ERRORCHECK &&
            muxPended->usMuxCount != 0 &&
            muxPended->pstOwner == runTsk) {
        return EDEADLK;
    }
    if ((INT8)(mutex->stAttr.type) < PTHREAD_MUTEX_NORMAL ||
        mutex->stAttr.type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.prioceiling) < OS_TASK_PRIORITY_HIGHEST ||
        mutex->stAttr.prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.protocol) < PTHREAD_PRIO_NONE ||
        mutex->stAttr.protocol > PTHREAD_PRIO_PROTECT) {
        return EINVAL;
    }

    ret = OsMuxPendForPosix(mutex, LOS_WAIT_FOREVER);

    return map_errno(ret);
}

//-----------------------------------------------------------------------------
// Try to lock mutex.
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
    MUX_CB_P *muxPended = (MUX_CB_P *)NULL;
    LosTaskCB *runTsk = (LosTaskCB *)NULL;
    UINT32 ret;

    if (mutex == NULL) {
        return EINVAL;
    }

    if (OS_INT_ACTIVE) {
        return EPERM;
    }

#if (LOSCFG_BASE_CORE_SWTMR == YES)
      /* DO NOT Call blocking API in software timer task */
    if (g_losTask.runTask->taskEntry == (TSK_ENTRY_FUNC)OsSwtmrTask)
    {
        PRINTK("Warning: DO NOT call %s in software timer callback.\n", __FUNCTION__);
        osBackTrace();
    }
#endif

    muxPended = &mutex->stLock;
    runTsk = g_losTask.runTask;

    if (mutex->stLock.pstOwner != NULL && mutex->stLock.pstOwner != runTsk) {
        return EBUSY;
    }
    if (mutex->stAttr.type != PTHREAD_MUTEX_RECURSIVE && muxPended->usMuxCount != 0) {
        return EBUSY;
    }
    if ((INT8)(mutex->stAttr.type) < PTHREAD_MUTEX_NORMAL ||
        mutex->stAttr.type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.prioceiling) < OS_TASK_PRIORITY_HIGHEST ||
        mutex->stAttr.prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.protocol) < PTHREAD_PRIO_NONE ||
        mutex->stAttr.protocol > PTHREAD_PRIO_PROTECT) {
        return EINVAL;
    }

    ret = OsMuxPendForPosix(mutex, 0);

    return map_errno(ret);
}


//-----------------------------------------------------------------------------
// Unlock mutex.
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
    MUX_CB_P *pstMuxPosted = (MUX_CB_P *)NULL;
    LosTaskCB *pstRunTsk = (LosTaskCB *)NULL;
    UINT32 uwRet;

    if (mutex == NULL) {
        return EINVAL;
    }

    if (OS_INT_ACTIVE) {
        return EPERM;
    }

#if (LOSCFG_BASE_CORE_SWTMR == YES)
     /* DO NOT Call blocking API in software timer task */
    if (g_losTask.runTask->taskEntry == (TSK_ENTRY_FUNC)OsSwtmrTask)
    {
        PRINTK("Warning: DO NOT call %s in software timer callback.\n", __FUNCTION__);
        osBackTrace();
    }
#endif

    pstMuxPosted = &mutex->stLock;
    pstRunTsk = g_losTask.runTask;

    if (pstMuxPosted->usMuxCount == 0) {
        return EPERM;
    }

    if (pstMuxPosted->pstOwner != pstRunTsk) {
        return EPERM;
    }
    if ((INT8)(mutex->stAttr.type) < PTHREAD_MUTEX_NORMAL ||
        mutex->stAttr.type > PTHREAD_MUTEX_ERRORCHECK) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.prioceiling) < OS_TASK_PRIORITY_HIGHEST ||
        mutex->stAttr.prioceiling > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }
    if ((INT8)(mutex->stAttr.protocol) < PTHREAD_PRIO_NONE ||
        mutex->stAttr.protocol > PTHREAD_PRIO_PROTECT) {
        return EINVAL;
    }

    uwRet = osMuxPostForPosix(mutex);

    return map_errno(uwRet);
}

static UINT32 OsMuxPendForPosix(pthread_mutex_t *mutex, UINT32 timeout)
{
    UINT32 intSave;
    UINT32 retErr;
    UINT16 bitMapPri;
    MUX_CB_P *muxPended = &mutex->stLock;
    LosTaskCB *runTsk = (LosTaskCB *)NULL;
    LosTaskCB *pendedTask = (LosTaskCB *)NULL;
    LOS_DL_LIST *pendObj    = (LOS_DL_LIST *)NULL;

    intSave = LOS_IntLock();
    if (muxPended->stMuxList.pstPrev == NULL ||
        muxPended->stMuxList.pstNext == NULL) {
        //this is for mutex macro initialization
        muxPended->usMuxCount  = 0;
        muxPended->pstOwner    = (LosTaskCB *)NULL;
        LOS_ListInit(&muxPended->stMuxList);
    }
    runTsk = g_losTask.runTask;
    if (muxPended->usMuxCount == 0) {
        muxPended->usMuxCount++;
        muxPended->pstOwner = runTsk;
        osMuxDlockNodeInsertHook(runTsk->taskID, muxPended);

        if (runTsk->priority > mutex->stAttr.prioceiling
            && mutex->stAttr.protocol == PTHREAD_PRIO_PROTECT) {
            LOS_BitMapSet(&runTsk->priBitMap, runTsk->priority);
            OsTaskPriModify(runTsk, mutex->stAttr.prioceiling);
        }

        LOS_IntRestore(intSave);
        return LOS_OK;
    }

    if (muxPended->pstOwner == runTsk
        && mutex->stAttr.type == PTHREAD_MUTEX_RECURSIVE) {
        muxPended->usMuxCount++;
        LOS_IntRestore(intSave);
        return LOS_OK;
    }

    if (g_losTaskLock) {
        retErr = LOS_ERRNO_MUX_PEND_IN_LOCK;
        goto ERRRE_UNIMUXPEND;
    }

    /* while g_losTask.runTask is not changed, we can use local variable 'pRunTsk' for performance */
    OsPriqueueDequeue(&runTsk->pendList);
    runTsk->taskStatus &= (~OS_TASK_STATUS_READY);
    runTsk->taskMux = (VOID *)muxPended;
    pendObj = &runTsk->pendList;
    runTsk->taskStatus |= OS_TASK_STATUS_PEND;

    if (muxPended->pstOwner->priority > runTsk->priority
        && mutex->stAttr.protocol == PTHREAD_PRIO_INHERIT) {
        LOS_BitMapSet(&(muxPended->pstOwner->priBitMap), muxPended->pstOwner->priority);
        OsTaskPriModify(muxPended->pstOwner, runTsk->priority);
    }

    if (LOS_ListEmpty(&muxPended->stMuxList)) {
        LOS_ListTailInsert(&muxPended->stMuxList, pendObj);
    } else {
        if ((pendedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&muxPended->stMuxList))) && /*lint !e413*/
            pendedTask->priority > runTsk->priority) {
            LOS_ListHeadInsert(&muxPended->stMuxList, pendObj);
        } else if ((pendedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_LAST(&muxPended->stMuxList))) && /*lint !e413*/
            pendedTask->priority <= runTsk->priority) {
            LOS_ListTailInsert(&muxPended->stMuxList, pendObj);
        } else {
            LOS_DL_LIST_FOR_EACH_ENTRY(pendedTask, (&(muxPended->stMuxList)), LosTaskCB, pendList) {/*lint !e413*/
                if (pendedTask->priority < runTsk->priority) {
                    continue;
                } else if (pendedTask->priority > runTsk->priority) {
                    LOS_ListAdd(pendedTask->pendList.pstPrev, pendObj);
                    break;
                } else {
                    LOS_ListAdd(&pendedTask->pendList, pendObj);
                    break;
                }
            }
        }
    }

    if (timeout != LOS_WAIT_FOREVER) {
        runTsk->taskStatus |= OS_TASK_STATUS_PEND_TIME;
        OsTaskAdd2TimerList((LosTaskCB *)runTsk, timeout);
        (VOID)LOS_IntRestore(intSave);
        LOS_Schedule();

        intSave = LOS_IntLock();
        if (muxPended->pstOwner->priority == runTsk->priority) {
            if ((bitMapPri = LOS_LowBitGet(muxPended->pstOwner->priBitMap)) != LOS_INVALID_BIT_INDEX) {
                LOS_BitMapClr(&(muxPended->pstOwner->priBitMap), bitMapPri);
                OsTaskPriModify(muxPended->pstOwner, bitMapPri);
            }
        } else {
            if (LOS_HighBitGet(muxPended->pstOwner->priBitMap) != runTsk->priority) {
                LOS_BitMapClr(&(muxPended->pstOwner->priBitMap), runTsk->priority);
            }
        }

        (VOID)LOS_IntRestore(intSave);
    } else {
        LOS_IntRestore(intSave);
        LOS_Schedule();

    }

    if (runTsk->taskStatus & OS_TASK_STATUS_TIMEOUT) {
        intSave = LOS_IntLock();
        runTsk->taskStatus &= (~OS_TASK_STATUS_TIMEOUT);
        (VOID)LOS_IntRestore(intSave);
        retErr = LOS_ERRNO_MUX_TIMEOUT;
        goto ERROR_UNIMUXPEND;
    }

    return LOS_OK;

ERRRE_UNIMUXPEND:
    (VOID)LOS_IntRestore(intSave);
ERROR_UNIMUXPEND:
    return retErr;
}

static UINT32 osMuxPostForPosix(pthread_mutex_t *mutex)
{
    UINT32 uwIntSave;
    UINT16 usBitMapPri;
    MUX_CB_P *pstMuxPosted = &mutex->stLock;
    LosTaskCB *pstResumedTask = (LosTaskCB *)NULL;
    LosTaskCB *pstPendedTask  = (LosTaskCB *)NULL;
    LosTaskCB *pstRunTsk      = (LosTaskCB *)NULL;

    uwIntSave = LOS_IntLock();
    pstRunTsk      = g_losTask.runTask;
    if (--(pstMuxPosted->usMuxCount) != 0
        && mutex->stAttr.type == PTHREAD_MUTEX_RECURSIVE) {
        LOS_IntRestore(uwIntSave);
        return LOS_OK;
    }

    if (mutex->stAttr.protocol == PTHREAD_PRIO_PROTECT)
    {
        if ((usBitMapPri = LOS_HighBitGet(pstRunTsk->priBitMap)) != LOS_INVALID_BIT_INDEX)
        {
            LOS_BitMapClr(&pstRunTsk->priBitMap, usBitMapPri);
            OsTaskPriModify(pstRunTsk, usBitMapPri);
        }
    }

    /* whether a task block the mutex lock*/
    if (!LOS_ListEmpty(&pstMuxPosted->stMuxList)) {
        pstResumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(pstMuxPosted->stMuxList)));/*lint !e413*/
        LOS_ListDelete(LOS_DL_LIST_FIRST(&(pstMuxPosted->stMuxList)));
        LOS_ASSERT_COND(pstResumedTask->taskStatus & OS_TASK_STATUS_PEND);

        pstResumedTask->taskStatus &= (~OS_TASK_STATUS_PEND);
        if (pstResumedTask->taskStatus & OS_TASK_STATUS_PEND_TIME) {
            OsTimerListDelete(pstResumedTask);
            pstResumedTask->taskStatus &= (~OS_TASK_STATUS_PEND_TIME);

        }

        if (pstResumedTask->priority != pstRunTsk->priority)
        {
            if (LOS_HighBitGet(pstRunTsk->priBitMap) != pstResumedTask->priority)
                LOS_BitMapClr(&pstRunTsk->priBitMap, pstResumedTask->priority);
        }
        else
        {
            if (pstRunTsk->priBitMap != 0)
            {
                if (!LOS_ListEmpty(&pstMuxPosted->stMuxList))
                {
                    usBitMapPri = LOS_HighBitGet(pstRunTsk->priBitMap);
                    LOS_DL_LIST_FOR_EACH_ENTRY(pstPendedTask, (&(pstMuxPosted->stMuxList)), LosTaskCB, pendList) /*lint !e413*/
                    {
                        if (usBitMapPri != pstPendedTask->priority)
                            LOS_BitMapClr(&pstRunTsk->priBitMap, pstPendedTask->priority);
                    }
                }

                usBitMapPri = LOS_LowBitGet(pstRunTsk->priBitMap);
                LOS_BitMapClr(&pstRunTsk->priBitMap, usBitMapPri);
                OsTaskPriModify(pstMuxPosted->pstOwner, usBitMapPri);
            }
        }

        pstMuxPosted->usMuxCount = 1;
        pstMuxPosted->pstOwner = pstResumedTask;
        pstResumedTask->taskMux = NULL;
        osMuxDlockNodeDeleteHook(pstRunTsk->taskID, pstMuxPosted);
        osMuxDlockNodeInsertHook(pstResumedTask->taskID, pstMuxPosted);

        if (!(pstResumedTask->taskStatus & OS_TASK_STATUS_SUSPEND)) {
            pstResumedTask->taskStatus |= OS_TASK_STATUS_READY;
            OsPriqueueEnqueue(&pstResumedTask->pendList, pstResumedTask->priority);
        }

        (VOID)LOS_IntRestore(uwIntSave);
        LOS_Schedule();
    } else {
        pstMuxPosted->pstOwner = (LosTaskCB *)NULL;
        osMuxDlockNodeDeleteHook(pstRunTsk->taskID, pstMuxPosted);
        (VOID)LOS_IntRestore(uwIntSave);
    }

    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
